package cn.gson.school.controller.zhaosheng;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.school.dao.xitong.DeptSerchDao;
import cn.gson.school.dao.xitong.UserDao;
import cn.gson.school.dao.xitong.UserInfoDao;
import cn.gson.school.dao.zhaosheng.InputStudentDao;
import cn.gson.school.dao.zhaosheng.TranferassistDao;
import cn.gson.school.entity.xitong.DeptEntity;
import cn.gson.school.entity.xitong.UserEntity;
import cn.gson.school.entity.xitong.UserInfoEntity;
import cn.gson.school.entity.zhaosheng.InputStudentEntity;
import cn.gson.school.entity.zhaosheng.TranferassistEntity;
import cn.gson.school.mappers.zhaosheng.TranferassistMapper;

/**
 * 移交协助信息Controller
 */
@Controller
@RequestMapping("/zs-transferass")
public class TransferassController {
	@Autowired
	private TranferassistMapper tranferassistMapper;

	@Autowired
	private TranferassistDao tranferassistDao;

	@Autowired
	private InputStudentDao inputStudentDao;

	@Autowired
	private UserInfoDao userinfoDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private DeptSerchDao deptDao;

	@Autowired
	private UserInfoDao userInfoDao;

	@RequestMapping("list")
	@ResponseBody
	public Map<String, Object> index(TranferassistEntity tranferassistEntity,
			HttpServletRequest request,
			@RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize) {
		Map<String, Object> obj =new HashMap<String, Object>();
		HttpSession session=request.getSession();
		UserInfoEntity u=(UserInfoEntity) session.getAttribute("userinfo");
		PageHelper.startPage(pageNum, pageSize);
		tranferassistEntity.setTranferId(u.getUserInfoId());
		List<TranferassistEntity> pageData = (Page<TranferassistEntity>) tranferassistMapper.list(tranferassistEntity);
		if(pageData!=null) {
			PageInfo<TranferassistEntity> info = new PageInfo<>(pageData);
			obj = new HashMap<String, Object>();
			obj.put("total", info.getTotal());
			obj.put("rows", info.getList());
			
		}
		return obj;
	}

	@GetMapping("delete/{id}")
	@ResponseBody
	public JSONObject delete(@PathVariable("id") Long id) {
		JSONObject obj = new JSONObject();
		obj.put("success", false);
		TranferassistEntity u;
		if (id != null && (u = tranferassistDao.findOne(id)) != null) {
			tranferassistDao.delete(u);
			obj.put("success", true);
		} else {
			obj.put("message", "此用户不存在！");
		}
		return obj;
	}

	/**
	 * 表单
	 */
	@GetMapping({ "zs-transferass-add", "zs-transferass-add/{id}" })
	public String form(@PathVariable(name = "id", required = false) Long id, Model model) {
		System.out.println("学生id：" + id);
		DeptEntity dept=deptDao.findOne(3L);
		List<UserInfoEntity> allUserName=userInfoDao.findByDept(dept);
		if (id != null) {
			TranferassistEntity tran = tranferassistDao.findOne(id);
			InputStudentEntity stu=inputStudentDao.findOne(id);
			if (tran != null && stu!=null) {
				model.addAttribute("tranferassistEntity", tran);
				model.addAttribute("inputStuEntity", stu);
				model.addAttribute("consultant", allUserName);
			}
		}
		model.addAttribute("consultant", allUserName);
		return "zhaosheng/zs-transferass-add";
	}

	/**
	 * 添加移交信息
	 */
	@PostMapping("save")
	@ResponseBody
	public Map<String, Object> save(@Valid TranferassistEntity tranferassistEntity, BindingResult br,
			HttpServletRequest request) {
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		if (br.hasErrors()) {
			obj.put("message", "信息校验失败！");
			obj.put("erros", br.getFieldErrors());
		} else {
			System.out.println("值：" + request.getParameter("tranIsreceived"));
			System.out.println("用户id：" + request.getParameter("userid"));

			String userName = request.getParameter("requestuserinfoName");// 获取被请求的咨询师的姓名
			String inputStuName = request.getParameter("inputStuName");// 获取学生姓名

			Integer tranIsreceived = Integer.parseInt(request.getParameter("tranIsreceived"));

			InputStudentEntity student = inputStudentDao.findByInputStuName(inputStuName);// 找到学生
			UserInfoEntity requestuser = userinfoDao.findByUserName(userName);// 找到被请求的咨询师

			Long userId = Long.parseLong(request.getParameter("userid"));// 当前咨询师id
			UserEntity useraccount = userDao.findOne(userId);// 找到当前咨询师账号
			UserInfoEntity user = userinfoDao.findByUserId(useraccount);// 找到当前咨询师

			if (tranferassistEntity.getTranferId() == null) {
				if (inputStuName == null) {
					obj.put("message", "学生姓名不能为空！");
					return obj;
				} else if (userName == null) {
					obj.put("message", "被请求咨询师姓名不能为空！");
					return obj;
				}
				tranferassistEntity.setInputstuName(student);
				tranferassistEntity.setUserName(requestuser);
				tranferassistEntity.setTranIsreceived(tranIsreceived);
				tranferassistEntity.setTranTime((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
				tranferassistEntity.setUserinfoName(user);

			} else {
				// 编辑
				tranferassistEntity.setInputstuName(student);
				tranferassistEntity.setUserName(requestuser);
				tranferassistEntity.setTranIsreceived(tranIsreceived);
				tranferassistEntity.setTranTime((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
				tranferassistEntity.setUserinfoName(user);
			}
			tranferassistDao.save(tranferassistEntity);
			obj.put("success", true);
		}
		return obj;
	}
}
